package com.example.meetsmart.repository;

import com.example.meetsmart.model.Room;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.time.LocalDateTime;
import java.util.List;

@Repository
public interface RoomRepository extends JpaRepository<Room, Long> {
    List<Room> findByIsActiveTrue();

    @Query("SELECT r FROM Room r WHERE r.isActive = true AND r.capacity >= :capacity " +
            "AND r.hasProjector = :hasProjector AND r.hasWhiteboard = :hasWhiteboard " +
            "AND r.hasAudioSystem = :hasAudioSystem")
    List<Room> findAvailableRoomsWithCriteria(
            @Param("capacity") int capacity,
            @Param("hasProjector") boolean hasProjector,
            @Param("hasWhiteboard") boolean hasWhiteboard,
            @Param("hasAudioSystem") boolean hasAudioSystem);

    @Query("SELECT r FROM Room r WHERE r.id NOT IN " +
            "(SELECT b.room.id FROM Booking b WHERE " +
            "b.status = 'APPROVED' AND " +
            "((b.startTime <= :endTime AND b.endTime >= :startTime))) " +
            "AND r.isActive = true")
    List<Room> findAvailableRoomsBetweenTimes(
            @Param("startTime") LocalDateTime startTime,
            @Param("endTime") LocalDateTime endTime);
}